﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.YouTube;


namespace YoutubeVideoManager
{
    /// <summary>
    /// Logica di interazione per AuthenticationWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        #region Properties
        #endregion

        #region Fields
        /// <summary>
        /// Permette il login asincrono
        /// </summary>
        BackgroundWorker worker;
        #endregion

        #region Methods
        public LoginWindow()
        {
            InitializeComponent();

            // Inizializza il backgroundworker
            worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        }

        /// <summary>
        /// Effettua il login.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Viene lanciata una eccezione se non riesco a connettermi
            YouTubeService service = new YouTubeService("", "");
            service.setUserCredentials(Global.Username, Global.Password);

            Global.Request = null;
            Global.IsLogged = true;
            try
            {
                string token = service.QueryClientLoginToken();

                // Se arrivo qui, allora il login va bene
                YouTubeRequestSettings settings = new YouTubeRequestSettings("", "", Global.Username, Global.Password);
                Global.Request = new YouTubeRequest(settings);
            }
            catch
            {
                Global.IsLogged = false;
            }
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (Global.IsLogged)
            {
                MainWindow mw = new MainWindow();
                mw.Show();
                this.Close();
            }
            else
            {
                this.IsEnabled = true;
                this.prgAsync.Visibility = System.Windows.Visibility.Collapsed;

                //altrimenti chiedo nuovamente utente e password.
                this.Title = "Inserire nome utente e password:";
                txtUsername.Background = txtPassword.Background = Brushes.Red;
            }
        }
        private void cmdLogin_Click(object sender, RoutedEventArgs e)
        {
            txtUsername.Background = txtPassword.Background = Brushes.White;
            if (string.IsNullOrWhiteSpace(txtUsername.Text))
            {
                txtUsername.Background = Brushes.Red;
            }
            else if (string.IsNullOrWhiteSpace(txtPassword.Password))
            {
                txtPassword.Background = Brushes.Red;
            }
            else
            {
                //Aggiorno user & password
                Global.Username = txtUsername.Text;

                // TODO : Usare un sistema più sicuro
                Global.Password = txtPassword.Password;

                //tento il login
                Login();
            }
        }
        private void cmdCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Login();
        }

        void Login()
        {
            this.IsEnabled = false;
            this.prgAsync.Visibility = System.Windows.Visibility.Visible;

            worker.RunWorkerAsync();
        }
    }
}
